package com.uusee.crawler.job;

import java.util.Random;

import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;

import com.uusee.crawler.dbwriter.UgcDbWriter;
import com.uusee.crawler.fastwriter.UgcFastWriter;
import com.uusee.crawler.fetcher.FetchHTTP;
import com.uusee.crawler.framework.AbstractJob;
import com.uusee.crawler.model.CrawlURI;
import com.uusee.crawler.pageprocessor.video.youku.YoukuGetPlayListInterfacePageProcessor;
import com.uusee.crawler.xmlwriter.UgcXmlWriter;
import com.uusee.shipshape.sp.model.Ugc;

public class YoukuUgcFastCrawlJob extends AbstractJob {
	private FetchHTTP fetch = new FetchHTTP();
	private YoukuGetPlayListInterfacePageProcessor pageProcessor = new YoukuGetPlayListInterfacePageProcessor();
	private UgcXmlWriter xmlwriter = new UgcXmlWriter();
	private UgcDbWriter dbWriter = new UgcDbWriter();
	private UgcFastWriter fastWriter = new UgcFastWriter("test1");
	
	public static void main(String[] args) {
		try {
			ApplicationContext acx = new ClassPathXmlApplicationContext("applicationContext-ugc.xml");
			YoukuUgcFastCrawlJob youkuUgcFastCrawlJob = (YoukuUgcFastCrawlJob)acx.getBean("youkuUgcFastCrawlJob");
			youkuUgcFastCrawlJob.doExecute();
		} catch (Exception e) {
			e.printStackTrace();
		}
	}
	public CrawlURI createCrawlURI(String vid) {
		Ugc ugc = new Ugc();
		ugc.setVid(vid+"");
		ugc.setVideoSite("www.youku.com");

		Random r = new Random();
		int ni = r.nextInt(9);
		int ran = ni * 1000;
		String crawlUrl = "http://v.youku.com/player/getPlayList/VideoIDS/" + vid
				+ "/version/5/source/video/password/?ran=" + ran + "&n=3";
		
		CrawlURI crawlURI = new CrawlURI();
		crawlURI.setSourceSite("www.youku.com");
		crawlURI.setCrawlUrl(crawlUrl);
		crawlURI.setModel(ugc);
		crawlURI.setCharset("utf-8");
		
		return crawlURI;
	}
	
	public void crawl(CrawlURI crawURI) {
		try {
			fetch.process(crawURI);
			pageProcessor.process(crawURI);
			//xmlwriter.process(crawURI);
			dbWriter.process(crawURI);
			fastWriter.process(crawURI);
		} catch(Exception e) {
			e.printStackTrace();
		}
	}

	public void setFetch(FetchHTTP fetch) {
		this.fetch = fetch;
	}

	public void setPageProcessor(YoukuGetPlayListInterfacePageProcessor pageProcessor) {
		this.pageProcessor = pageProcessor;
	}

	public void setXmlwriter(UgcXmlWriter xmlwriter) {
		this.xmlwriter = xmlwriter;
	}

	public void setDbWriter(UgcDbWriter dbWriter) {
		this.dbWriter = dbWriter;
	}
}
